package dom;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

public class Phone3 {
    public static void main(String[] args) {
        //得到DOM解析器的工厂实例
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        //从DOM工厂获得DOM解析器
        DocumentBuilder db= null;
        try {
            db = dbf.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
        //解析XML文档,得到一个Document对象,即DOM树
        Document doc= null;
        try {
            doc = db.parse("src/xml/Phone.xml");
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //读取pubDate
        NodeList list=doc.getElementsByTagName("pubDate");
        //pubDate元素节点
        Element pubDateElement=(Element)list.item(0);
        //读取文本节点
        String pubDate=pubDateElement.getFirstChild().getNodeValue();
        System.out.println(pubDate);
    }
}
